//
// Copyright (C) 2011 Steve Channell steve.channell@cepheis.com
//
// This file is part of Cephei.QL, an open-source library wrapper 
// arround QuantLib http://quantlib.org/
//
// Cephei.QL is open source software: you can redistribute it and/or modify it
// under the terms of the license.  You should have received a
// copy of the license along with this program; if not, please email
// <support@cepheis.com>. The license is also available online at
// <http://cepheis.com/license.htm>.
//
// This program is distributed in the hope that it will be useful, but WITHOUT
// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
// FOR A PARTICULAR PURPOSE.  See the license for more details.
//
// Version 2.101
//#include "stdafx.h"
#include "MTBrownianGenerator.h"
using namespace Cephei::QL::Models::Marketmodels::Browniangenerators;
#include <gen/QL/Models/Marketmodels/BrownianGenerator.h>
using namespace Cephei::QL::Models::Marketmodels;
#undef HANDLE
#undef ABSTRACT
#undef STRUCT
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::CMTBrownianGenerator (boost::shared_ptr<QuantLib::MTBrownianGenerator>& childNative, Object^ owner) : CBrownianGenerator(CMTBrownianGenerator::typeid)
{
#ifdef HANDLE
	_phMTBrownianGenerator = NULL;
#endif
	_ppMTBrownianGenerator = &childNative;
    _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppMTBrownianGenerator));
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::CMTBrownianGenerator (QuantLib::MTBrownianGenerator& childNative, Object^ owner) : CBrownianGenerator(CMTBrownianGenerator::typeid)
{
#ifdef HANDLE
	_phMTBrownianGenerator = NULL;
#endif
	_ppMTBrownianGenerator = new boost::shared_ptr<QuantLib::MTBrownianGenerator> (&childNative);
    _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppMTBrownianGenerator));
    _MTBrownianGeneratorOwner = owner;
    _BrownianGeneratorOwner = owner;
}

Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::CMTBrownianGenerator (CMTBrownianGenerator^ copy) : CBrownianGenerator(CMTBrownianGenerator::typeid)
{
#ifdef HANDLE
	_phMTBrownianGenerator = NULL;
#endif
	if (copy->HasNative() != NULL)
    {
		_ppMTBrownianGenerator = new boost::shared_ptr<QuantLib::MTBrownianGenerator> (copy->GetShared());
        _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppMTBrownianGenerator));
    }
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::CMTBrownianGenerator (System::Type^ t) : CBrownianGenerator(CMTBrownianGenerator::typeid)
{
#ifdef HANDLE
	_phMTBrownianGenerator = NULL;
#endif
	if (!t->IsSubclassOf(CMTBrownianGenerator::typeid))
		throw gcnew Exception ("Invalid base-case init");
}
#ifdef HANDLE
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::CMTBrownianGenerator (QuantLib::Handle<QuantLib::MTBrownianGenerator>& childNative, Object^ owner)  : CBrownianGenerator(CMTBrownianGenerator::typeid)
{
	_phMTBrownianGenerator = &childNative;
	_ppMTBrownianGenerator = &static_cast<boost::shared_ptr<QuantLib::MTBrownianGenerator>>(childNative.currentLink());
    _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppMTBrownianGenerator));
    _MTBrownianGeneratorOwner = owner;
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::CMTBrownianGenerator (QuantLib::Handle<QuantLib::MTBrownianGenerator> childNative)  : CBrownianGenerator(CMTBrownianGenerator::typeid)
{
	_phMTBrownianGenerator = &childNative;
	_ppMTBrownianGenerator = &static_cast<boost::shared_ptr<QuantLib::MTBrownianGenerator>>(childNative.currentLink());
    _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppMTBrownianGenerator));
}
#endif
#ifdef STRUCT
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::CMTBrownianGenerator (QuantLib::MTBrownianGenerator childNative)  : CBrownianGenerator(CMTBrownianGenerator::typeid)
{
#ifdef HANDLE
	_phMTBrownianGenerator = NULL;
#endif
	_ppMTBrownianGenerator = new boost::shared_ptr<QuantLib::MTBrownianGenerator> (new QuantLib::MTBrownianGenerator (childNative));
    _ppBrownianGenerator = new boost::shared_ptr<QuantLib::BrownianGenerator> (boost::dynamic_pointer_cast<QuantLib::BrownianGenerator> (*_ppMTBrownianGenerator));
}
#endif

Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::~CMTBrownianGenerator ()
{
    if (_ppMTBrownianGenerator != NULL)
    {
	    delete _ppMTBrownianGenerator;
        _ppMTBrownianGenerator = NULL;
    }
}
Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::!CMTBrownianGenerator ()
{
    if (_ppMTBrownianGenerator != NULL)
    {
	    delete _ppMTBrownianGenerator;
    }
}
QuantLib::MTBrownianGenerator& Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::GetReference ()
{
    if (_ppMTBrownianGenerator == NULL) throw gcnew NativeNullException ();
	return **_ppMTBrownianGenerator;
}
boost::shared_ptr<QuantLib::MTBrownianGenerator>& Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::GetShared ()
{
    if (_ppMTBrownianGenerator == NULL) throw gcnew NativeNullException ();
	return *_ppMTBrownianGenerator;
}
QuantLib::MTBrownianGenerator* Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::GetPointer ()
{
    if (_ppMTBrownianGenerator == NULL) throw gcnew NativeNullException ();
	return &**_ppMTBrownianGenerator;
}
#ifdef HANDLE
QuantLib::Handle<QuantLib::MTBrownianGenerator>& Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::GetHandle ()
{
	if (_phMTBrownianGenerator == NULL)
	{
		_phMTBrownianGenerator = new Handle<QuantLib::MTBrownianGenerator> (*_ppMTBrownianGenerator);
	}
	return *_phMTBrownianGenerator;
}
#endif
bool Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::HasNative () 
{
	return (_ppMTBrownianGenerator != NULL);
}

Double Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::NextPath::get ()
{
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMTBrownianGenerator)->nextPath ( );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
Double Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::NextStep (Cephei::IVector<Double>^ output)
{
    CoVector<Double>^ _Coutput;
    try
    {
    	boost::detail::spinlock::scoped_lock lock (*_pSpinlock);
        _Coutput = safe_cast<CoVector<Double>^> (output);
        _Coutput->Lock();
        INativeVector<Double>^ _NCIoutput = _Coutput->getFeature (NativeFeature::Value);
        CDoubleVector^ _NCoutput = safe_cast<CDoubleVector^>(_NCIoutput);
        std::vector<QuantLib::Real>& _output = static_cast<std::vector<QuantLib::Real>&> (_NCoutput->GetReference ());
    	QuantLib::Real _rv = (QuantLib::Real)(*_ppMTBrownianGenerator)->nextStep ( _output );   
        Double _nrv = (Double)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
        if (_Coutput != nullptr) _Coutput->Unlock();
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::NumberOfFactors::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMTBrownianGenerator)->numberOfFactors ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
UInt64 Cephei::QL::Models::Marketmodels::Browniangenerators::CMTBrownianGenerator::NumberOfSteps::get ()
{
    try
    {
    	QuantLib::Size _rv = (QuantLib::Size)(*_ppMTBrownianGenerator)->numberOfSteps ( );   
        UInt64 _nrv = (UInt64)ValueHelper::Convert (_rv);
    	return _nrv;
	}
    catch (QuantLib::Error& _error)
    {
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown quantlib error"));
        
    }
	catch (std::exception& _error)
	{
        if (_error.what())
		    throw gcnew NativeExcpetion (gcnew System::String(_error.what()));
        else
		    throw gcnew NativeExcpetion (gcnew System::String("Unknown std::exception"));
	}
    finally
    {
    }
}
//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// Factory class

